//
//  LNTableViewCell.m
//  DemoRATree
//
//  Created by xiangism on 15/9/7.
//  Copyright (c) 2015年 LiteNice. All rights reserved.
//

#import "LNTableViewCell.h"

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface LNTableViewCell()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (nonatomic) BOOL isFile;

@end


@implementation LNTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (UIColor*) intRed:(int)red intGreen:(int)green intBlue:(int)blur
{
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blur/255.0 alpha:1.0];
}

- (void)setupWithTitle:(NSString*)title level:(NSInteger)level isFile:(BOOL)isFile {
    self.nameLabel.text = title;
    self.isFile = isFile;
  
    if (isFile) {
        self.backgroundColor = [UIColor whiteColor];
        self.iconImage.image = [UIImage imageNamed:@"note"];
        
    } else {
        self.iconImage.image = [UIImage imageNamed:@"dir"];
        if (level == 0) {
            self.backgroundColor = [self intRed:198 intGreen:230 intBlue:201];
        } else if (level == 1) {
            self.backgroundColor = [self intRed:198 intGreen:240 intBlue:201];
        } else if (level == 2) {
            self.backgroundColor = [self intRed:198 intGreen:250 intBlue:201];
        } else if (level == 3) {
            self.backgroundColor = [self intRed:198 intGreen:250 intBlue:210];
        } else if (level == 4) {
            self.backgroundColor = [self intRed:198 intGreen:250 intBlue:230];
        } else if (level >= 5) {
            self.backgroundColor = [self intRed:198 intGreen:250 intBlue:250];
        }
    }
    
    CGFloat left = 5 + 20*level;
    CGRect iconFrame = self.iconImage.frame;
    iconFrame.origin.x = left;
    self.iconImage.frame = iconFrame;
    
    left += 20;
    
    CGRect titleFrame = self.nameLabel.frame;
    titleFrame.origin.x = left;
    self.nameLabel.frame = titleFrame;
    
    //[self setLongPress];
}

- (void)setLongPress {
    [self becomeFirstResponder]; //必须
    [self addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)]];
}

- (void)longTap:(UILongPressGestureRecognizer*)guesture
{
    if (guesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Longlonglonglong");
        
        UIMenuController *menu=[UIMenuController sharedMenuController];
        UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"新建" action:@selector(copyItemClicked:)];
        UIMenuItem *resendItem = [[UIMenuItem alloc] initWithTitle:@"重命名" action:@selector(resendItemClicked:)];
        UIMenuItem *deleteItem = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(resendItemClicked:)];
        UIMenuItem *moveItem = [[UIMenuItem alloc] initWithTitle:@"移动" action:@selector(resendItemClicked:)];
        
        [menu setMenuItems:[NSArray arrayWithObjects:copyItem,resendItem, deleteItem, moveItem, nil]];
        [menu setTargetRect:self.bounds inView:self];
        [menu setMenuVisible:YES animated:YES];
    }
}

// 必须
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    if (action ==@selector(copyItemClicked:)) {
        return YES;
    } else if (action==@selector(resendItemClicked:)) {
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}
//实现成为第一响应者方法
-(BOOL)canBecomeFirstResponder{
    return YES;
}

-(void)resendItemClicked:(id)sender{
    NSLog(@"转发");
    //通知代理
}
-(void)copyItemClicked:(id)sender{
    NSLog(@"复制");
    // 通知代理
}

- (void)setIsExpand:(BOOL)isExpand {
    if (!self.isFile) {
        if (isExpand) {
            self.iconImage.image = [UIImage imageNamed:@"dir_open"];
            
        } else {
            self.iconImage.image = [UIImage imageNamed:@"dir"];
        }
    }
}

@end







